/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var search = function(nums, target) {
  let len = nums.length;
  for(let i=0;i<len;i++){
      if(nums[i]==target) return i; 
      if(nums[i]>nums[i+1]){
          if(nums[len-1]<target)   return binarySearch(0,i,nums,target);
          else return binarySearch(i+1,len-1,nums,target);
      }
  }
  return -1;
};

var binarySearch = function(l,r,nums,target){
  let mid = Math.floor((l+r)/2);
  while(l<=r){
      if(nums[mid]==target) return mid;
      if(nums[mid]>target) r = mid-1;
      else l = mid+1;
      mid = Math.floor((l+r)/2);
  }
  return -1;
}